package cn.ecnu.utils;

import cn.ecnu.vo.common.Pagination;

public class PaginationUtil {
    public static Pagination generate(Integer currentPage, Integer maxPage, Integer minPage, Integer totalSize, String prefix) {
        Pagination pagination = new Pagination(); // 首先获取分页
        pagination.setCurrentPage(currentPage); // 设置当前页面
        pagination.setHasNext(currentPage < maxPage); pagination.setHasPrev(currentPage > minPage);
        if (pagination.getHasNext()) {
            pagination.setNextUrl(new UrlGenerator(prefix, currentPage + 1).getUrl());
        }
        if (pagination.getHasPrev()) {
            pagination.setPrevUrl(new UrlGenerator(prefix, currentPage - 1).getUrl());
        }
        pagination.setTotalSize(totalSize);
        return pagination;
    }

    public static Pagination paging(Integer pageNum, Integer totalSize, Integer pageSize, String prefix) {
        if (pageNum == null) pageNum = 1;
        pageNum = Math.max(1, pageNum); // 首先预处理pageNum, 最小为1;
        int maxPage = (totalSize - 1) / pageSize + 1;
        pageNum = Math.min(pageNum, maxPage) ; // 最大为totalSize / 20 + 1
        return generate(pageNum, maxPage, 1, totalSize, prefix);
    }
}
